//
//  LBBuyCarController.m
//  loveBeen
//
//  Created by 杨烁 on 2016/11/19.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBBuyCarController.h"

#import "LBEmptyBuyCarView.h"

#import "LBBuyCardetailsCell.h"
#import "LBPlusMinusTool.h"
#import "LBSuperMarketProducts.h"
#import "LBBuyCardetailsFootCell.h"
#import "LBOrderRemarkController.h"
#import "LBPayMoneyController.h"
#import "LBAddressLastView.h"
#import "LBAdressFirstView.h"


@interface LBBuyCarController ()<LBEmptyBuyCarViewDelegate,UITableViewDataSource,UITableViewDelegate,LBBuyCardetailsFootCellDelegate>
@property(assign,nonatomic)int change;

@property(weak,nonatomic)UITableView  *tableView;

@property(weak,nonatomic)LBBuyCardetailsFootCell *  footercell;
@end

@implementation LBBuyCarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backValue:) name:@"backValue" object:nil];
    
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(customLoad) name:@"refrash" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(PushEmpty) name:@"PushEmpty" object:nil];
    
    
    
    self.navigationItem.title = @"购物车";
    
}

//备注界面跳转
-(void)PushDetails{
    
    LBOrderRemarkController *remarkVc = [[LBOrderRemarkController alloc]init];
    
   
    
    [self.navigationController pushViewController:remarkVc animated:YES];
    
}


//push没有购物数据界面
-(void)PushEmpty{
    LBEmptyBuyCarView *emptyView = [[LBEmptyBuyCarView alloc]initWithFrame:self.view.bounds];
    emptyView.delegate = self;
    [self.view addSubview:emptyView];
    
}

//跳转到支付界面
-(void)PushPay:(LBBuyCardetailsFootCell *)LBBuyCardetailsFootCell{
    LBPayMoneyController *pay = [[LBPayMoneyController alloc]init];
    
    [self.navigationController pushViewController:pay animated:YES];
    
}

-(void)setupUI{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.rowHeight = 80;
    tableView.separatorStyle = NO;
    
    self.tableView = tableView;
    
    [self.view addSubview:tableView];
    //    tableView.tableFooterView = [[UIView alloc]init];
    [tableView registerNib:[UINib nibWithNibName:@"LBBuyCardetailsCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [tableView registerNib:[UINib nibWithNibName:@"LBAdressFirstView" bundle:nil] forCellReuseIdentifier:@"cell2"];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section == 0){//如果没有取到数据,将第一行取消显示
        return 1;
    }
    return [LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSArray<LBSuperMarketProducts *> *array = [LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel;
    
    if (indexPath.section ==0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        return cell;
    }
    
    if(indexPath.section ==1)
    {
        LBBuyCardetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        LBSuperMarketProducts *model = array[indexPath.row];
        cell.model = model;
        
        return cell;
    }else{
        return [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];}
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if(section ==1){
        
        //合并xib写法
        UIView *titleView =  [[[NSBundle mainBundle]loadNibNamed:@"LBBuyCardetailsHeadCell" owner:self options:nil]firstObject];
        
        UIView *receiveTime =  [[[NSBundle mainBundle]loadNibNamed:@"LBBuyCardetailsHeadCell" owner:self options:nil]lastObject];
        
        
        //分写xib
        //        UIView *receiveTime = [[[NSBundle mainBundle]loadNibNamed:@"LBBuyCarDetailsTimeCell" owner:self options:nil]lastObject];
        
        UIView *view = [[UIView alloc]init];
        
        [view addSubview:titleView];
        [view addSubview:receiveTime];
        
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(view);
            make.height.offset(40);
        }];
        [receiveTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleView.mas_bottom);
            make.left.bottom.right.equalTo(view);
        }];
        
        
        return view;
    }
    
    return nil;
}




-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    if(section ==1){
        
        //合并xib写法
        
        LBBuyCardetailsFootCell *remark=  [[[NSBundle mainBundle]loadNibNamed:@"LBBuyCardetailsFootCell" owner:self options:nil]firstObject];
        
        self.footercell = remark;
        
        remark.lb_msg.text = @"其他要求   (如带红塔山一盒)";
//        remark.lb_msg.text = @"string";
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(PushDetails)];
        
        [remark addGestureRecognizer:tap];
        
        LBBuyCardetailsFootCell *checkOut =  [[[NSBundle mainBundle]loadNibNamed:@"LBBuyCardetailsFootCell" owner:self options:nil]lastObject];
        
        checkOut.delegate = self;
        LBPlusMinusTool *manager = [LBPlusMinusTool sharedPlusMinusTool];
        
        
        //判断footerView 全选按钮....
        checkOut.selected = [manager isAllSelected];
        checkOut.lb_total.text = [NSString stringWithFormat:@"共 ¥%.2f",manager.selectedProductPrice];
        
        
        
        if([manager isSelected]&&manager.selectedProductsModel.count > 0 ){
            
            checkOut.sendBtn.userInteractionEnabled = YES;
            [checkOut.sendBtn setBackgroundImage:[UIImage imageNamed:@"v2_my_avatar_bg"] forState:UIControlStateNormal];
            
            [checkOut.sendBtn setTitle:@"选好了" forState:UIControlStateNormal];
            [checkOut.sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        else{
            checkOut.sendBtn.userInteractionEnabled = NO;
            [checkOut.sendBtn setBackgroundImage:[UIImage imageNamed:@"grey1"] forState:UIControlStateNormal];
            [checkOut.sendBtn setTitle:@"满减起送" forState:UIControlStateNormal];
            [checkOut.sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
        }
        
        UIView *view = [[UIView alloc]init];
        
        [view addSubview:remark];
        [view addSubview:checkOut];
        
        [remark mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(view);
            make.height.offset(50);
        }];
        [checkOut mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(remark.mas_bottom);
            make.left.bottom.right.equalTo(view);
        }];
        
        
        return view;
    }
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        return 100;
    }
    return 10;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        return 100;
    }
    return 10;
}



-(void)LBEmptyBuyCarView:(LBEmptyBuyCarView *)buyCarView{
    
    [buyCarView removeFromSuperview];
    [self.tabBarController setSelectedIndex:0];
    
    
}




//将要出现的时候判断一下
-(void)viewWillAppear:(BOOL)animated{
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
    
    [self.tableView reloadData];
    
    if(![LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel.count){
        LBEmptyBuyCarView *emptyView = [[LBEmptyBuyCarView alloc]initWithFrame:self.view.bounds];
        
        emptyView.delegate = self;
        [self.view addSubview:emptyView];
        
    }
}

-(void)customLoad{
    
    [self.tableView reloadData];
    
    //    NSLog(@"已经load");
}

//视图消失后.如果子控件大于1,就删除盖在上面的控件(因为将要出现的时候会添加视图)
-(void)viewDidDisappear:(BOOL)animated{
    
    if (self.view.subviews.count>1) {
        
        [self.view.subviews[1] removeFromSuperview];
    }
}



-(void)backValue:(NSNotification *)notification{
    
    NSDictionary *dic = notification.userInfo;
    
    self.footercell.lb_msg.text = dic[@"text"];
    
    [LBPlusMinusTool sharedPlusMinusTool].os = dic[@"text"];
    
    [self.tableView reloadData];
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        
        [LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel[indexPath.row].totalCount--;
        
        [[LBPlusMinusTool sharedPlusMinusTool].selectedProductsModel removeObjectAtIndex:indexPath.row];
        
        [LBPlusMinusTool sharedPlusMinusTool].change = YES;
        // Delete the row from the data source.
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        return;
    }
}
//如果购物车减完没有商品,就执行
@end
